package ecoll.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import ecoll.client.centers.Center;
import ecoll.client.centers.Function;
import ecoll.client.centers.NewsCenter;
import ecoll.client.centers.PersonalDataCenter;
import ecoll.client.centers.TextProcessingCenter;
import ecoll.client.centers.VocabularyChallengeCenter;

public class MainMenu extends VerticalPanel {

	private static final Center[] MENU = new Center[] { new NewsCenter(),
			new PersonalDataCenter(), new TextProcessingCenter(),
			new VocabularyChallengeCenter() };

	public MainMenu() {

		for (int i = 0; i < MENU.length; ++i) {
			final Center center = MENU[i];
			Label header = new Label(center.name());
			header.setStyleName("menuItem");
			header.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					center.execute();
				}
			});
			if(center.function(0) == null) {
				add(header);
				continue;
			}
			VerticalPanel subMenu = new VerticalPanel();
			for (int j = 0; center.function(j) != null; ++j) {
				final Function function = center.function(j);
				Label label = new Label(function.name());
				label.setStyleName("subMenuItem");
				label.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						function.execute();
					}
				});
				subMenu.add(label);
			}
			DisclosurePanel panel = new DisclosurePanel();
			panel.setHeader(header);
			panel.setContent(subMenu);
			add(panel);
		}
	}
}
